﻿using Knight.Web.Infrastructure;
using Knight.Web.Infrastructure.Menus;
using Knight.Web.Infrastructure.Mvc;

using Knight.Models.Entity;
using Knight.Models.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApp.Areas.Manage.Controllers.Filters;
using Knlght.Domain;

namespace WebApp.Areas.Manage.Controllers
{
    public class NewsController : BaseAdminController
    {
        public void GetAllChildren(List<NewsType> all, NewsType curItem)
        {
            if (all == null) return;
            var subItems = all.Where(ee => ee.ParentId == curItem.Id).ToList();
            if (subItems.Count > 0)
            {
                curItem.Nodes = new List<NewsType>();
                curItem.Nodes.AddRange(subItems);
            }
            foreach (var subItem in subItems)
            {
                GetAllChildren(all, subItem);
            }
        }
        public void GetAllNodes(List<NewsType> list, int parentId, List<int> node)
        {
            if (list == null)
                return;
            var sublist = parentId != 0 ? list.Where(t => t.ParentId == parentId).ToList() : list.Where(o => o.ParentId == parentId).ToList();
            if (!sublist.Any())
                return;
            foreach (var item in sublist)
            {
                node.Add(item.Id);
                GetAllNodes(list, item.Id, node);
            }
        }

        [MallAdminMenu("News_Type")]
        [Permission("News_Type")]
        public ActionResult TypeIndex()
        {
            var list = getDefaultService().GetList<NewsType>().Models;
            //实例化一个根节点
            var rootRoot = new NewsType
            {
                Id = 0,
                ParentId = 0,
                Name = "根节点"
            };
            GetAllChildren(list, rootRoot);
            ViewBag.TreeDatas = rootRoot.Nodes;
            ViewBag.TypeList = list;
            return View();
        }

        [MallAdminMenu("News_Info")]
        [Permission("News_Info")]
        public ActionResult Index()
        {          
            ViewBag.TypeList = getDefaultService().GetList<NewsType>().Models;
            return View();
        }


        #region AngularJs
        [HttpPost]
        public JsonResult AddType(NewsType model)
        {
            var service = new NewsTypeService();
            var rsp = service.Add(model);
            return EJActionJson(rsp);
        }
        [HttpPost]
        public JsonResult UpdateType(NewsType model)
        {
            var service = getDefaultService();
            var rsp = service.Update(model);
            return EJActionJson(rsp);
        }
        [HttpPost]
        public JsonResult RemoveType(int id)
        {
            var service = getDefaultService();
            var rsp = service.GetModel<NewsType>(id);
            if (rsp.IsSuccess)
            {
                var str = new List<int>();
                var list = getDefaultService().GetList<NewsType>().Models;
                GetAllNodes(list, rsp.Model.Id, str);
                str.Add(id);
                var rsp2 = service.Remove<NewsType>(o => str.Contains(o.Id));
                return EJActionJson(rsp2);

            }
            return EJActionJson();
        }


        const string AdUploadDirectory = "newsinfo";
        /// <summary>
        /// 上传文件
        /// </summary>
        [HttpPost]
        public JsonResult Upload()
        {
            var rsp = this.UploadPics(AdUploadDirectory);
            return EJItemJson(rsp);
        }
        [HttpPost]
        public JsonResult PagingNewsInfo(PagingParameter parameter)
        {
            var query = new QueryRequest<NewsInfo>();
            if (parameter.Query.CreateCriterionByString(nameof(NewsInfo.Title)) != null)
            {
                var username = parameter.Query.CreateCriterionByString(nameof(NewsInfo.Title)).Value.ToString();
                query.Add(o => o.Title.Contains(username));
            }
            if (parameter.Query.CreateCriterionByBoolean(nameof(NewsInfo.IsShow)) != null)
            {
                var isShow = Convert.ToBoolean(parameter.Query.CreateCriterionByBoolean(nameof(NewsInfo.IsShow)).Value);
                query.Add(o => o.IsShow == isShow);
            }            
            if (parameter.Query.CreateCriterionByBoolean(nameof(NewsInfo.IsTop)) != null)
            {
                var isTop = Convert.ToBoolean(parameter.Query.CreateCriterionByBoolean(nameof(NewsInfo.IsTop)).Value);
                query.Add(o => o.IsTop == isTop);
            }
            if (parameter.Query.CreateCriterionByBoolean(nameof(NewsInfo.IsHome)) != null)
            {
                var isHome = Convert.ToBoolean(parameter.Query.CreateCriterionByBoolean(nameof(NewsInfo.IsHome)).Value);
                query.Add(o => o.IsHome == isHome);
            }
            var service = getDefaultService();
            query.AddOrder(nameof(NewsInfo.Title), true);
            query.AddOrder(nameof(NewsInfo.AddTime), false);
            var pagingRequest = new PagingRequest<NewsInfo>
            {
                PageIndex = parameter.PageIndex,
                PageSize = parameter.PageSize,
                Query = query
            };
            var rsp = service.Paging(pagingRequest);
            return EJPagingJson(rsp);
        }
        [HttpPost]
        public JsonResult AddNewsInfo(NewsInfo model, string tempImageUrl)
        {
            model.ImageUrl = UploadHelper.MoveFile(tempImageUrl);
            var service = getDefaultService();
            var rsp = service.Add(model);
            return rsp.IsSuccess ? EJActionJson(true, model.Id.ToString()) : EJActionJson(rsp);
        }
        [HttpPost]
        public JsonResult UpdateNewsInfo(NewsInfo model, string tempImageUrl)
        {
            model.ImageUrl = UploadHelper.MoveFile(tempImageUrl);
            var service = getDefaultService();
            var rsp = service.Update(model);
            return EJActionJson(rsp);
        }

        [HttpPost]
        public JsonResult UpdatesNewsInfo(int[] ids, string property, object value)
        {
            var service = getDefaultService();
            if (property.ToLower() == nameof(NewsInfo.IsShow).ToLower())
            {              
                var rsp = service.UpdateToList<NewsInfo>(o => ids.Contains(o.Id), i => new NewsInfo { IsShow = (bool)value });
                return EJActionJson(rsp);
            }            
            else if (property.ToLower() == nameof(NewsInfo.IsHome).ToLower())
            {             
                var rsp = service.UpdateToList<NewsInfo>(o => ids.Contains(o.Id), i => new NewsInfo { IsHome = (bool)value });
                return EJActionJson(rsp);
            }
            else if (property.ToLower() == nameof(NewsInfo.IsTop).ToLower())
            {             
                var rsp = service.UpdateToList<NewsInfo>(o => ids.Contains(o.Id), i => new NewsInfo { IsTop = (bool)value });
                return EJActionJson(rsp);
            }
            else
            {
                return EJActionJson(false, "该属性修改方法没有实现！");
            }
        }
        [HttpPost]
        public JsonResult RemoveNewsInfo(int id)
        {
            var service = getDefaultService();
            var rsp = service.Remove<NewsInfo>(o => o.Id == id);
            return EJActionJson(rsp);
        }
        [HttpPost]
        public JsonResult RemovesNewsInfo(int[] ids)
        {
            var service = getDefaultService();
            var rsp = service.Remove<NewsInfo>(o => ids.Contains(o.Id));
            return EJActionJson(rsp);
        }
        #endregion

    }
}